import configEnv from '@/configEnv.js'
const COOKIE_EXPIRE_TIME = configEnv.COOKIE_EXPIRE_TIME

export default {
	/**
	* 设置 cookie，带过期时间
	*/
	set(key, value, expire = COOKIE_EXPIRE_TIME) {
		const now = Date.now();
		uni.setStorageSync(key, {
			value,
			expireTime: now + expire
		});
	},

	/**
	* 获取 cookie，若已过期则返回 null 并清除
	*/
	get(key) {
		const data = uni.getStorageSync(key);
		if (!data || !data.expireTime) return null;

		const now = Date.now();
		if (now > data.expireTime) {
			this.remove(key);
			return null;
		}

		return data.value;
	},

	/**
	* 移除指定 cookie
	*/
	remove(key) {
	uni.removeStorageSync(key);
	},

	/**
	* 清空所有 cookie
	*/
	clear() {
	uni.clearStorageSync();
	}
};